<?php
/**
 * app端，预加载店铺下的所有素材文件
 * Author: lyun
 * Date: 2019/3/15
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\App;

use App\Http\Controllers\ApiController;
use App\Models\Customer\Asset;
use App\Models\Store\Store;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PreloadAssetController extends ApiController
{
    const ROUTES = [
        'index' => 'api.app.preload.asset.index',
    ];

    /**
     * app端，预加载店铺下的所有素材文件
     * test
     *
     * @return array|\Illuminate\Http\Response
     */
    public function index()
    {
        $device_account = Guard::deviceAccount();
        /** @var Store $store */
        $store = $device_account->store;

        $customer_id = $store->customer_id;
        $store_id = $store->id;

        /** @var Collection $assets */
        $assets = (new Asset)
            ->where('customer_id', $customer_id)
            ->whereIn('store_id', [$store_id, 0])
            ->get();

        $assets->each(function (Asset $asset) {
            $xml = $asset->getOriginal('model_xml');
            $model_xml = $asset->model_xml;

            $asset->xml_key = $xml;
            if ($asset->isGlass()) {
                $asset->model_glass_android = str_replace('{*}', 'Android', $model_xml);
                $asset->model_glass_ios = str_replace('{*}', 'IOS', $model_xml);
            }
        });

        return $this->response(compact('assets'));
    }
}
